<template>
  <div>
    <div class="topContainer">
      <div class="user-profile-head-banner"></div>
      <div class="user-profile-head-info">
        <div class="user-profile-head-info-t">
          <div class="avatarBox">
            <div class="avatar">
              <img
                v-if="userInfo.avatar_url"
                :src="$url + userInfo.avatar_url"
                alt=""
              />
            </div>
          </div>
          <div class="userInfo">
            <div class="usernameBox">{{ userInfo.username }}</div>
            <div class="topicsBox">总创作文章:{{ userInfo.num }}</div>
          </div>
          <div class="buttonBox">
            <el-button
              class="editBtn"
              v-if="loginUserId == userInfo.user_id"
              plain
              @click="toEditUser"
              >编辑资料</el-button
            >
          </div>
        </div>
      </div>
    </div>
    <div class="mainContainer">
      <div class="navList">
        <el-select
          v-model="type"
          size="mini"
          clearable
          placeholder="请选择分类"
          @change="selectChanged"
        >
          <el-option
            v-for="item in types"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </div>
      <div class="articleBox">
        <div class="article" v-for="item in topics" :key="item.id">
          <div class="article_l">
            <div class="titleBox" @click="toArticle(item.topic_id)">
              {{ item.title }}
            </div>
            <div class="contentBox">
              {{ returnText(item.content) }}
            </div>
            <div class="footerBox">
              <div class="timeBox">发布博客于 {{ item.created_time }} ·</div>
              <div class="view">{{ item.views }} 阅读 ·</div>
              <div class="comment">{{ item.comments.length }} 评论</div>
            </div>
          </div>
          <div class="article_r" v-if="loginUserId == userInfo.user_id">
            <span style="font-size: 14px; margin-right: 5px; color: #aaa"
              >章节状态:</span
            >
            <el-tag
              :type="item.state == 1 ? 'primary' : 'danger'"
              style="margin-right: 30px"
              >{{ item.state == 1 ? '公开' : '私密' }}</el-tag
            >
            <div class="hoverBox">
              <el-button
                type="success"
                plain
                @click="toChangeTopic(item.topic_id)"
                >修改文章</el-button
              >
              <el-popconfirm
                style="margin-left: 20px"
                title="这是一段内容确定删除吗？"
                @confirm="deleteTopic(item.topic_id)"
              >
                <template #reference>
                  <el-button type="danger" plain>删除文章</el-button>
                </template>
              </el-popconfirm>
            </div>
          </div>
        </div>
      </div>
      <div class="pagination" style="padding-left: 30px; margin-top: 20px">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total,  prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <div class="bottom"></div>
  </div>
</template>

<script>
import { findOneUser } from '../api/user';
import { delTopic, getTopicTypes } from '../api/topic';
import { mapState } from 'vuex';

export default {
  data() {
    return {
      pageSize: 5,
      currentPage: 1,
      user_id: '',
      userInfo: {},
      total: 0,
      type: '',
      types: [],
      topics: [],
    };
  },
  computed: {
    ...mapState({ loginUserId: 'user_id', loginLevel: 'user_level' }),
  },
  methods: {
    handleCurrentChange(page) {
      this.currentPage = page;
      this.selectChanged();
    },
    async getUserInfo() {
      const res = await findOneUser(this.user_id);
      if (res.code !== 200) return;
      this.userInfo = res.data;
      this.userInfo.num = res.data.topic.length;
      if (this.user_id != this.loginUserId) {
        this.userInfo.topic = res.data.topic.filter((item) => {
          return item.state == 1;
        });
      } else {
        this.userInfo.topic = res.data.topic;
      }
      this.total = this.userInfo.topic.length;
      this.selectChanged();
    },
    async getTypes() {
      const res = await getTopicTypes();
      if (res.code != 200) {
        this.$message.error('获取分类列表错误，请刷新页面');
      }
      this.types = res.data;
    },
    selectChanged() {
      if (this.type) {
        let topicArr = [...this.userInfo.topic];
        this.topics = topicArr.filter((item) => {
          return item.type == this.type;
        });
        this.total = this.topics.length;
        let offset = (this.currentPage - 1) * this.pageSize;
        this.topics = this.topics.slice(offset, offset + this.pageSize);
      } else {
        this.topics = [...this.userInfo.topic];
        this.total = this.topics.length;
        let offset = (this.currentPage - 1) * this.pageSize;
        this.topics = this.topics.slice(offset, offset + this.pageSize);
      }
    },
    returnText(htmlStr) {
      var text = htmlStr.replace(/<[^<>]+>/g, '');
      return text;
    },
    toChangeTopic(topic_id) {
      if (this.loginLevel >= this.userInfo.user_level) {
        this.$router.push({
          name: 'create',
          params: {
            update: true,
            topic_id,
          },
        });
      }
    },
    toEditUser() {
      this.$router.push('/edit');
    },
    toArticle(topic_id) {
      this.$router.push({
        name: 'article',
        params: {
          topic_id,
        },
      });
    },
    async deleteTopic(topic_id) {
      const res = await delTopic(topic_id);
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: '删除文章成功',
        });
        this.getUserInfo();
      }
    },
  },
  created() {
    this.user_id = this.$route.params.user_id;
    this.getUserInfo();
    this.getTypes();
  },
};
</script>

<style lang="less" scoped>
.topContainer {
  background-image: url('../assets/img/user_top.png');
  background-repeat: no-repeat;
  background-position: top;
  background-size: 1920px 100px;
  margin: auto;
  width: 1280px;
  .user-profile-head-banner {
    padding-top: 16px;
    min-width: 1000px;
    margin: auto;
    height: 100px;
    background-repeat: no-repeat;
    background-position: 50%;
    background-size: cover;
  }
  .user-profile-head-info {
    padding-left: 16px;
    padding-right: 24px;
    background: #fff;
    box-sizing: border-box;
    min-width: 1000px;
    margin: 0 auto;
    border-radius: 2px;
    .user-profile-head-info-t {
      display: flex;

      .avatarBox {
        position: relative;
        width: 102px;
        height: 90px;
        .avatar {
          position: absolute;
          top: -12px;
          width: 102px;
          height: 102px;
          border: 4px solid #f0f0f2;
          background: #fff;
          border-radius: 50%;
          img {
            border-radius: 50%;
            width: 100%;
          }
        }
      }
      .userInfo {
        padding-top: 10px;
        padding-left: 20px;
        font-size: 16px;
        color: #666;
        .topicsBox {
          margin-top: 30px;
          color: #999;
        }
      }
      .buttonBox {
        padding-top: 30px;
        margin-left: auto;
      }
    }
  }
}
.mainContainer {
  min-width: 1000px;
  margin-top: 20px;
  margin-left: auto;
  margin-right: auto;
  width: 1280px;
  background-color: #fff;
  .navList {
    height: 48px;
    line-height: 48px;
    background: #fff;
    margin-left: 24px;
    padding-top: 10px;
  }
  .articleBox {
    .article {
      padding: 24px;
      border-bottom: 1px solid #f0f0f2;
      display: flex;
      &:hover {
        background-color: rgb(247, 239, 239);
      }
      &:hover .article_r .hoverBox {
        display: block;
      }
      .article_l {
        flex-grow: 1;

        .titleBox {
          cursor: pointer;
          &:hover {
            color: #fc5531 !important;
          }
          font-size: 18px;
          font-weight: 500;
          line-height: 24px;
          color: #222226;
          overflow: hidden;
          white-space: normal;
          word-break: break-word;
          -webkit-box-orient: vertical;
          display: -webkit-box;
          -webkit-line-clamp: 1;
        }
        .contentBox {
          color: #555666;
          margin-top: 8px;
          line-height: 24px;
          overflow: hidden;
          white-space: normal;
          word-break: break-word;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
        }
        .footerBox {
          display: flex;
          margin-top: 10px;
          * {
            color: #555666;
            line-height: 20px;
            font-size: 14px;
            margin-right: 8px;
          }
        }
      }
      .article_r {
        display: flex;
        justify-content: center;
        align-items: center;
        .hoverBox {
          display: none;
        }
      }
    }
  }
}
.bottom {
  height: 30px;
  margin-top: 30px;
}
</style>
